package com.xhcl.system.service.impl;

import java.util.List;
import java.util.ArrayList;

import com.xhcl.common.core.domain.Stree;
import com.xhcl.common.core.domain.Ztree;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xhcl.system.mapper.TGgKmdmMapper;
import com.xhcl.system.mapper.TGgYskjkmgjMapper;
import com.xhcl.system.domain.TGgKmdm;
import com.xhcl.system.domain.TGgPtdwgj;
import com.xhcl.system.domain.TGgYskjkmgj;
import com.xhcl.system.service.ITGgKmdmService;
import com.xhcl.common.core.text.Convert;
import com.xhcl.common.exception.BusinessException;

/**
 * 会计科目Service业务层处理
 * 
 * @author xhcl
 * @date 2020-08-25
 */
@Service
public class TGgKmdmServiceImpl implements ITGgKmdmService 
{
    @Autowired
    private TGgKmdmMapper tGgKmdmMapper;
    
    @Autowired
    private TGgYskjkmgjMapper tGgYskjkmgjMapper;

    /**
     * 查询会计科目
     * 
     * @param nd 会计科目ID
     * @return 会计科目
     */
    @Override
    public TGgKmdm selectTGgKmdmById(String kmdm)
    {
        return tGgKmdmMapper.selectTGgKmdmById(kmdm);
    }

    /**
     * 查询会计科目列表
     * 
     * @param tGgKmdm 会计科目
     * @return 会计科目
     */
    @Override
    public List<TGgKmdm> selectTGgKmdmList(TGgKmdm tGgKmdm)
    {
        return tGgKmdmMapper.selectTGgKmdmList(tGgKmdm);
    }

    /**
     * 新增会计科目
     * 
     * @param tGgKmdm 会计科目
     * @return 结果
     */
    @Override
    public int insertTGgKmdm(TGgKmdm tGgKmdm)
    {
    	TGgKmdm qryGgKmdm=new TGgKmdm();
    	qryGgKmdm.setNd(tGgKmdm.getNd());
    	qryGgKmdm.setKmdm(tGgKmdm.getKmdm());
    	List<TGgKmdm> list=tGgKmdmMapper.selectTGgKmdmList(qryGgKmdm);
    	if (list!=null && list.size()>0) {
    		throw new BusinessException("科目代码已存在");
    	}
        return tGgKmdmMapper.insertTGgKmdm(tGgKmdm);
    }

    /**
     * 修改会计科目
     * 
     * @param tGgKmdm 会计科目
     * @return 结果
     */
    @Override
    public int updateTGgKmdm(TGgKmdm tGgKmdm)
    {
        return tGgKmdmMapper.updateTGgKmdm(tGgKmdm);
    }

    /**
     * 删除会计科目对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTGgKmdmByIds(String ids)
    {
        return tGgKmdmMapper.deleteTGgKmdmByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除会计科目信息
     * 
     * @param nd 会计科目ID
     * @return 结果
     */
    @Override
    public int deleteTGgKmdmById(String kmdm)
    {
        return tGgKmdmMapper.deleteTGgKmdmById(kmdm);
    }

    /**
     * 查询会计科目树列表
     * 
     * @return 所有会计科目信息
     */
    @Override
    public List<Stree> selectTGgKmdmTree()
    {
        List<TGgKmdm> tGgKmdmList = tGgKmdmMapper.selectTGgKmdmList(new TGgKmdm());
        List<Stree> ztrees = new ArrayList<Stree>();
        for (TGgKmdm tGgKmdm : tGgKmdmList)
        {
            Stree ztree = new Stree();
            ztree.setId(tGgKmdm.getKmdm());
            ztree.setpId(tGgKmdm.getSjkmdm());
            ztree.setName(tGgKmdm.getKmdm()+"-"+tGgKmdm.getKmmc());
            ztree.setTitle(tGgKmdm.getKmmc()+"|"+tGgKmdm.getFzhsdw());
            ztrees.add(ztree);
        }
        return ztrees;
    }
    
    @Override
    public List<Stree> selectTGgKmdmTree(Long nd)
    {   
    	TGgKmdm ggkmdm=new TGgKmdm();
    	if (nd!=null){
    		ggkmdm.setNd(nd);
    	}
        List<TGgKmdm> tGgKmdmList = tGgKmdmMapper.selectTGgKmdmList(ggkmdm);
        List<Stree> ztrees = new ArrayList<Stree>();
        for (TGgKmdm tGgKmdm : tGgKmdmList)
        {
            Stree ztree = new Stree();
            ztree.setId(tGgKmdm.getKmdm());
            ztree.setpId(tGgKmdm.getSjkmdm());
            ztree.setName(tGgKmdm.getKmdm()+"-"+tGgKmdm.getKmmc());
            ztree.setTitle(tGgKmdm.getKmmc()+"|"+tGgKmdm.getFzhsdw());
            ztrees.add(ztree);
        }
        return ztrees;
    }
    /**
     * 查询会计科目树列表
     * 
     * @return 所有会计科目信息
     */
    @Override
    public List<Stree> selectTGgKmdmTree(String yskmdm)
    {
      	List<TGgYskjkmgj> list=null;
    	if (StringUtils.isNotEmpty(yskmdm)) {
    		TGgYskjkmgj tGgYskjkmgj=new TGgYskjkmgj();
    		tGgYskjkmgj.setYskm(yskmdm);
	    	list=tGgYskjkmgjMapper.selectTGgYskjkmgjList(tGgYskjkmgj);
    	}
        List<TGgKmdm> tGgKmdmList = tGgKmdmMapper.selectTGgKmdmList(new TGgKmdm());
        List<Stree> ztrees = new ArrayList<Stree>();
        for (TGgKmdm tGgKmdm : tGgKmdmList)
        {
            Stree ztree = new Stree();
            ztree.setId(tGgKmdm.getKmdm());
            ztree.setpId(tGgKmdm.getSjkmdm());
            ztree.setName(tGgKmdm.getKmdm()+"-"+tGgKmdm.getKmmc());
            ztree.setTitle(tGgKmdm.getKmmc());
            if (list!=null) {
	        	ztree.setChecked(list.stream().filter(m->m.getKmdm().equals(tGgKmdm.getKmdm())).count()>0);
	        }
            ztrees.add(ztree);
        }
        return ztrees;
    }
    
    /**
     * 记账状态
     * @param tGgKmdm
     * @return
     */
    @Override
    public List<TGgKmdm> selectTGgKmdmForJzZtList(TGgKmdm tGgKmdm)
    {
    	if (tGgKmdm.getParams().get("sfjz")!=null && tGgKmdm.getParams().get("sfjz").equals("on")) {
    		return tGgKmdmMapper.selectTGgKmdmWjzList(tGgKmdm);
    	}else {
    		return tGgKmdmMapper.selectTGgKmdmYjzList(tGgKmdm);
    	}
    }
}
